package com.ysm.freestyle.viewmodel

import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.ysm.freestyle.utils.FileEntity
import com.ysm.freestyle.utils.ItemClassType
import com.ysm.freestyle.utils.TYPE_AUDIO_TEXT
import com.ysm.freestyle.utils.TYPE_VIDEO_TEXT
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.launch
import java.util.LinkedList

class FileScanViewModel : ViewModel() {
    // 对外暴露只读 StateFlow
    private val _flow = MutableSharedFlow<Int>()
    private val flow: SharedFlow<Int> = _flow.asSharedFlow()

    var scanFileList = ArrayList<FileEntity>()

    var scanResults = MutableLiveData<List<ScanResult>>()

    fun setData(fileEntity: FileEntity) {
        viewModelScope.launch {
            //扫描到文件，更新文件列表
            if(fileEntity.fileClass == ItemClassType.PHOTOS){
                val result = scanResults.value?.get(0)
                if(result!=null){
                    result.scanCount++
                    result.scanSize += fileEntity.fileSizeLong
                    result.data.add(fileEntity)
                }
                scanResults.postValue(scanResults.value)
            }
            if(fileEntity.fileClass == ItemClassType.VIDEOS){
                val result = scanResults.value?.get(1)
                if(result!=null){
                    result.scanCount++
                    result.scanSize += fileEntity.fileSizeLong
                    result.data.add(fileEntity)
                }
                scanResults.postValue(scanResults.value)
            }
            if(fileEntity.fileClass == ItemClassType.MUSIC){
                val result = scanResults.value?.get(2)
                if(result!=null){
                    result.scanCount++
                    result.scanSize += fileEntity.fileSizeLong
                    result.data.add(fileEntity)
                }
                scanResults.postValue(scanResults.value)
            }
            if(fileEntity.fileClass == ItemClassType.DOCUMENTS){
                val result = scanResults.value?.get(3)
                if(result!=null){
                    result.scanCount++
                    result.scanSize += fileEntity.fileSizeLong
                    result.data.add(fileEntity)
                }
                scanResults.postValue(scanResults.value)
            }

            if (fileEntity.isDocumentFile) {
                try {
                    if (fileEntity.fileSizeLong > 0) {
                        if (fileEntity.fileType == TYPE_VIDEO_TEXT || fileEntity.fileType == TYPE_AUDIO_TEXT) {
                            CoroutineScope(Dispatchers.IO).launch {}
                        }
                        scanFileList.add(fileEntity)
                    }
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            } else {
                if (TextUtils.isEmpty(fileEntity.filePath))
                    return@launch
                try {
                    if (fileEntity.fileSizeLong > 0 && !fileEntity.fileRealType.isNullOrEmpty()) {
                        if (fileEntity.fileType == TYPE_VIDEO_TEXT || fileEntity.fileType == TYPE_AUDIO_TEXT) {
                            CoroutineScope(Dispatchers.IO).launch {}
                        }
                        scanFileList.add(fileEntity)
                    }
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }
            _flow.emit(scanFileList.size)
        }
    }

    fun getFlow(): SharedFlow<Int> {
        return flow
    }


    fun initScanResultData(){
        scanResults.value = arrayListOf(
            ScanResult(scanType = 0, scanTypeName = "Phones"),
            ScanResult(scanType = 0, scanTypeName = "Videos"),
            ScanResult(scanType = 0, scanTypeName = "Music"),
            ScanResult(scanType = 0, scanTypeName = "Documents"),
            )

    }
}


data class ScanResult(
    var scanType :Int = 0,
    var scanTypeName : String = "",
    var scanCount : Int = 0,
    var scanSize:Long = 0,
    var data : LinkedList<FileEntity> = LinkedList()
)